package com.znf4.controller.ad;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import com.znf4.ad.entity.Ad;
import com.znf4.ad.server.AdServer;
import com.znf4.base.BaseReturnModel;
import com.znf4.constants.CommonConst;
import com.znf4.enums.OperationStatusEnum;
import com.znf4.id.IdGenerator;
import com.znf4.utils.OSSFile;

/**
 * 广告 http://123.206.66.232
 */
// @CrossOrigin(origins="http://127.0.0.1:8020") //高细粒度 配置跨域
@RestController
@RequestMapping("/ad")
public class AdController {

	private static final Logger LOG = LoggerFactory.getLogger(AdController.class);

	@Autowired
	private AdServer adServer;

	@Autowired
	private IdGenerator idGenerator;

	/**
	 * @return 函数功能说明 ： 添加广告
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/add", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> add(Ad ad, @RequestParam(value = "file", required = false)List<MultipartFile> files) {

		try {
			ad.setId(idGenerator.getAdNameNumber());
			String fileName = CommonConst.AD_IMG_NUMBER + ad.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					ad.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("广告上传图片到oss.....并设置数据库参数。");
				}
			}
			adServer.addAd(ad);
		} catch (Exception e) {
			LOG.info("添加广告信息异常 :" , e.getMessage());
			// JsonUtils.responseJson(response , BaseReturnModel.getMapModel("faild", "",
			// OperationStatusEnum.ERROR.getDesc() ) );
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
		// JsonUtils.responseJson(response ,BaseReturnModel.getMapModel("success",
		// "操作完成", "") );
	}

	/**
	 * 函数功能说明 ： 修改农作物商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/update", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> update(Ad ad , @RequestParam(value = "file", required = false)List<MultipartFile> files) {

		try {
			String fileName = CommonConst.AD_IMG_NUMBER + ad.getId();
			// 文件上传OSS
			for (MultipartFile multipartFile : files) {
				String orignalFileName = multipartFile.getOriginalFilename();
				if (multipartFile != null && orignalFileName != null && orignalFileName.length() > 0) {
					ad.setImg(OSSFile.upFile(multipartFile, fileName));
					LOG.info("广告上传图片到oss.....并设置数据库参数。");
				}
			}
			adServer.updateAd(ad);
		} catch (Exception e) {
			LOG.info("修改广告信息异常 :" , e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 删除广告
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/delOne", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delOne(Ad ad) {

		try {
			adServer.delOneAd(ad);
			// 删除文件 OSS
			OSSFile.deleteFile(ad.getImg());
		} catch (Exception e) {
			LOG.info("删除单个广告信息异常 :", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 删除多条农作物商品
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/delAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> delAll(String[] ids) {
		try {
			List<Ad> lAd = new ArrayList<>();
			for (int i = 0; i < ids.length; i++) {

				Ad ad = new Ad();
				ad.setId(ids[i]);
				lAd.add(ad);

				// 删除文件
				Ad delAd = adServer.queryOne(ad);
				OSSFile.deleteFile(delAd.getImg());

			}
			adServer.delAllAd(lAd);
		} catch (Exception e) {
			LOG.info("删除多个其他商品信息异常:", e.getMessage());
			return BaseReturnModel.getMapModel("faild", "", OperationStatusEnum.ERROR.getDesc());
		}
		return BaseReturnModel.getMapModel("success", "操作完成", "");
	}

	/**
	 * 函数功能说明 ： 获取所有广告
	 *
	 * @参数： @return @return String @throws
	 */
	@RequestMapping(value = "/queryAll", method = { RequestMethod.POST, RequestMethod.GET })
	public Map<String, Object> queryAll(Ad ad) {
		List<Ad> list = adServer.queryAll(ad);
		return BaseReturnModel.getMapModel("success", list, "");
	}

}
